const util = require('../../utils/util.js')
Component({
  observers: {
    'authorization': function(authorization) {}
  },

  properties: {
    parent_id: {
      type: String,
      value: '',
      observer: function(e) {
        this.setData({
          parent_id: e
        })
      }
    },
    bargain: {
      type: Boolean,
      observer: function(e) {
        this.setData({
          bargain: e
        })
      }
    },
    pid: {
      type: String,
      value: '',
      observer: function(e) {
        this.setData({
          pid: e
        })
      }
    },
    isShare: {
      type: Boolean,
      value: false,
      observer: function(e) {
        this.setData({
          isShare: e
        })
      }
    },
    ptItem: {
      type: Object,
      value: 0,
      observer: function(e) {
        this.setData({
          ptItem: e
        })
      }
    },
    market_type: {
      type: Number,
      value: 0,
      observer: function(e) {
        this.setData({
          market_type: e
        })
      }
    },
    deviation: {
      type: Number,
      value: 0,
      observer: function(e) {
        this.setData({
          deviation: e || ''
        })
      }
    },
    showSpecification: {
      type: Boolean,
      value: false,
      observer: function(e) {

      }
    },
    groupShop: {
      type: Boolean,
      value: false,
      observer: function(e) {
        this.setData({
          groupShop: true
        })
      }
    },
    normal: {
      type: Boolean,
      value: false,
      observer: function(e) {
        this.setData({
          normal: true,
          deviation: 1
        })
      }
    },
    seckill: {
      type: Boolean,
      value: false,
      observer: function(e) {
        this.setData({
          seckill: e
        })
      }
    },
    data: {
      type: Object,
      observer: function(data) {
        console.log(data, 'sdata')
        let placeType = [];
        data.sku_type.forEach((items, indexs) => {
          placeType.push(items.type)
        })
        this.setData({
          distribution_name: data.distribution_name,
          defaultIndex: data.defaultIndex,
          activeEndTime: data.activeEndTime,
          placeType,
          guigeDetails: data.low_sku ? data.low_sku : JSON.parse(data.sku)[0]
        })
        console.log(this.data.guigeDetails)
      }
    },
    skuTypeList: {
      type: Array,
      observer: function(data) {}
    }
  },
  data: {
    deviation: -1,
    lock: true,
    guigeDetails: '',
    imgUrl: getApp().globalData.imgUrl,
    imgUrls: getApp().globalData.imgUrls,
    buyNum: 1,
    isBuy: false
  },
  lifetimes: {
    ready() {},
  },
  methods: {
    bindGetUserInfo(e) {
      this.setData({
        pid: e.currentTarget.dataset.pid || 0,
        market_type: e.currentTarget.dataset.market_type || 0
      })
      if (e.detail.userInfo) {
        this.triggerEvent('myevent', {
          'showSpecification': !this.data.showSpecification
        })
        util.API('wechat/authorize/updateapp', {
          nick_name: e.detail.userInfo.nickName,
          cover: e.detail.userInfo.avatarUrl
        })
      } else {}
    },
    SpecificationIitem(e) {
      subscript = e.currentTarget.dataset, {
        data,
        skuTypeList
      } = this.data;

      let getguiges = JSON.parse(data.sku);
      skuTypeList = []
      data.sku_type.forEach((items, indexs) => {
        if (indexs == subscript.i) {
          items.children.forEach((item, index) => {
            if (index == subscript.is) {
              item.select = true
            } else {
              item.select = false
            }
          })
        }
        let sku_type = items.children.filter(itemF => {
          return itemF.select == true
        })
        if (sku_type[0]) {
          skuTypeList.push(parseInt(sku_type[0].valId))
        }
      })
      if (skuTypeList.length == data.sku_type.length) {
        getguiges.forEach((item, index) => {
          let newGuige = []
          item.sku_type = JSON.parse(item.sku_type)
          item.sku_type.forEach((items, indexs) => {
            for (prop in items) {
              newGuige.push(parseInt(prop))
            }
          })
          if (JSON.stringify(skuTypeList) == JSON.stringify(newGuige)) {
            this.setData({
              guigeDetails: getguiges[index],
              isBuy: true,
            })

          }
        })

      } else {
        this.setData({
          isBuy: false,
        })
      }
      this.setData({
        data: data,
      })
    },
    buyMinus: function() {
      if (this.data.buyNum >= 2) {
        this.setData({
          buyNum: --this.data.buyNum
        })
      }
    },
    buyAdd() {
      let that = this
      if (that.data.isBuy) {
        if (that.data.guigeDetails.sku_stock <= that.data.buyNum) {
          wx.showToast({
            title: '商品只剩这么多了',
            icon: 'none'
          })
          return false
        }
        that.setData({
          buyNum: ++that.data.buyNum
        })
      } else {
        wx.showToast({
          title: '请选择商品规格',
          duration: 1500,
          icon: 'none',
        })
      }
    },
    addCart(goods_id, goods_stock, sku_id, uniqueKey) {
      util.API('pyapi/add_googs_cart', {
        parent_id: this.data.parent_id,
        goods_id,
        goods_stock,
        sku_id,
        uniqueKey
      }, 'application/x-www-form-urlencoded').then(({
        msg
      }) => {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      })
    },


    buy: function(e) {
      let {
        lock,
        isBuy,
        data: {
          goods_id
        },
        guigeDetails,
        buyNum,
      } = this.data,
        ev = e.currentTarget.dataset;
      if (!lock) {
        return
      }
      if (!isBuy) {
        wx.showToast({
          title: '请选择规格',
          icon: 'none'
        })
        return false
      }
      if (!ev.puy) { //添加购物车
        this.addCart(goods_id, buyNum, guigeDetails.sku_id, guigeDetails.uniqueKey)
      } else {
        let {
          seckill, // 是不是秒杀
          groupShop, // 拼团
          pid, // 跟谁拼团的id
          market_type, // 订单类型0-->普通订单 && 分销订单 1-->拼团 2-->秒杀 3-->？ 4-->？ 5--> ？6-->0元购
          activeEndTime, // 活动截止时间
          deviation // 活动剩余时间
        } = this.data;
        if (groupShop) {
          if (market_type == 0) { // 点击原价购买
          } else {
            if (pid == 0) { // 点击成为团长
              if (activeEndTime < 0) { // 活动剩余时间
                wx.showToast({
                  title: '活动已到期',
                  icon: 'none'
                })
                return false
              }
            } else { // 点击去拼团
              let date = Date.now() / 1000,
                {
                  ptItem,
                  deviation
                } = this.data;
              if (ptItem) { // 选择了跟谁拼团
                if (ptItem.d_time < date) { // 每个人的活动剩余时间 小于当前的时间
                  if (deviation < 0) { // 活动剩余时间
                    wx.showToast({
                      title: '活动已到期',
                      icon: 'none'
                    })
                    return false
                  }
                }
              }
            }
          }
        }
        if (seckill) {
          if (deviation <= 0) {
            wx.showToast({
              title: '活动已到期',
              icon: 'none'
            })
            return false
          }
        }
        let way = this.data.data.distribution_type,
          shop_json = [{
            goods_id,
            goods_stock: buyNum,
            sku: guigeDetails.sku_id,
            uniqueKey: guigeDetails.uniqueKey
          }],
          {
            parent_id, // 分销上级的id
            defaultIndex, // 默认配送方式的索引
            distribution_name // 默认配送方式的名称
          } = this.data;
        wx.navigateTo({
          url: `/pages/DingdanPay/DingdanPay?goods=${JSON.stringify(shop_json)}&distribution=${way}&market_type=${market_type}&activity_id=${e.currentTarget.dataset.activity_id}&parent_id=${parent_id}&defaultIndex=${defaultIndex}&pid=${pid}&distribution_name=${distribution_name}`
        })
        // this.setData({
        //   isBuy: false
        // })
      }

    },
    bargain() {
      if (!this.data.isBuy) {
        wx.showToast({
          title: '请选择规格',
          icon: 'none'
        })
        return false
      }
      var pageinfo = util.Page(1)
      if (!pageinfo.data.suo) {
       
        pageinfo.setData({
          suo:true
        })
        util.API('bargain/v1.wxbargain/bargain_info', {
          uniqueKey: this.data.guigeDetails.uniqueKey,
          address_id: pageinfo.data.Address.address_id,
          goods_id: pageinfo.data.options.goods_id,
          bargain_id: pageinfo.data.options.bargain_id,
        }).then(({
          data,
          code,
          msg
        }) => {
        setTimeout(function(){
          pageinfo.setData({
            suo: false
          })
        },1000)
        console.log()
          if (code == 200) {
            wx.redirectTo({
              url: `/elsePage/pages/bargainDetails/bargainDetails?one_moeny=${data.one_moeny}&bargain_people_id=${data.bargain_people_id}`,
            })
          } else {
            wx.showToast({
              title: msg,
            })
          }
        })

      }

    }
  }
})